import copy
def augm_back(layer, insize, valueGen):
    """Augment layer from (so add extra values from) back."""
    layer = copy.deepcopy(layer)
    augment_size = insize - len(layer[0])
    for n in layer:
        n.append([valueGen() for s in xrange(augment_size)])
    return layer

def augm_front(layer, insize, valueGen):
    """Augment layer from (so add extra values from) back."""
    layer = copy.deepcopy(layer)
    augment_size = insize - len(layer[0])
    for n in xrange(len(layer)):
        layer[n] = [valueGen() for s in xrange(augment_size)] + layer[n]
    return layer



def merge(net1, net2, inpos2 = 0, valueGen):
    """Merges one network into another. Preferably one of the networks should
       have less layers than the other, or the output-size should be exactly
       the sum of output-sizes."""
    result = []
    prev_size = max(len(net1[0]), len(net2[0]) + inpos2)

    for layer in xrange(max(len(net1), len(net2)) - 1):
        next_prev_size = 0
        if layer < len(net1):
            result.append(augm_back(net1[layer]), prev_size, valueGen)
            next_prev_size += len(net1[layer])
        if layer < len(net2):
            result.append(augm_front(net2[layer], prev_size, valueGen)
            next_prev_size += len(net2[layer])

